﻿//======================================================================
//
//	XNA ContentBrowser
//
//  by Matt Pettineo (mpettineo@gmail.com)
//  05/03/09  
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;
using System.Collections.ObjectModel;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

using XNAPoint = Microsoft.Xna.Framework.Point;

namespace ContentBrowserSample
{


    public class MainView : Control
    {
        private Project project;
        private Stopwatch timer = new Stopwatch();
        private float currentTime = 0;

        public Project Project
        {
            get { return project; }
        }

        public MainView()
        {
                      
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            if (DesignMode)
                return;

            Application.Idle += new EventHandler(Application_Idle);
            timer.Start();

            project = new Project(Handle);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            // Override this to do nothing, so that the background doesn't get painted   
        }

        /// <summary>
        /// Redraws the control in response to a WinForms paint message.
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (DesignMode)
            {
                PaintUsingSystemDrawing(e.Graphics, this.GetType().Name);
                return;
            }

            if (project == null)
                return;

            // If the control was resized, reset the GraphicsDevice
            if (this.ClientSize.Width != project.GraphicsDeviceService.BackBufferWidth
                || this.ClientSize.Height != project.GraphicsDeviceService.BackBufferHeight)
                project.GraphicsDeviceService.SetBackBufferDimensions(ClientSize.Width, ClientSize.Height);
                                           
            float newTime = timer.ElapsedMilliseconds / 1000.0f;
            float dt = MathHelper.Clamp(newTime - currentTime, 0, 0.1f);
            currentTime = newTime;

            // Start up rendering
            if (project.GraphicsDeviceService.BeginDraw() == GraphicsDeviceStatus.Normal)
            {
                project.Render(dt);
                project.GraphicsDeviceService.EndDraw();
            }
            else
                PaintUsingSystemDrawing(e.Graphics, "GraphicsDevice not available");
        }

        /// <summary>
        /// If we do not have a valid graphics device (for instance if the device
        /// is lost, or if we are running inside the Form designer), we must use
        /// regular System.Drawing method to display a status message.
        /// </summary>
        private void PaintUsingSystemDrawing(System.Drawing.Graphics graphics, string text)
        {
            graphics.Clear(System.Drawing.Color.CornflowerBlue);

            using (Brush brush = new SolidBrush(System.Drawing.Color.Black))
            {
                using (StringFormat format = new StringFormat())
                {
                    format.Alignment = StringAlignment.Center;
                    format.LineAlignment = StringAlignment.Center;

                    graphics.DrawString(text, Font, brush, ClientRectangle, format);
                }
            }
        }

        void Application_Idle(object sender, EventArgs e)
        {
            // Invalidate on idle so that we get constant animation
            Invalidate();
        }
    }
}